home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / nktools.zip / STRFMT.ASM < prev    next >
Assembly Source File  |  1990-05-14  |  10KB  |  321 lines

  1. ;=======================================================================
  2. ; MODULE NAME:  StrFmt.ASM
  3. ; DEPENDENCIES: (None)
  4. ; LAST MOD ON:  9005.14
  5. ; PROGRAMMER:   Naoto Kimura
  6. ;
  7. ;     This is the assembly code for the routines in the StrUtil unit.
  8. ; Many of the routines were rewritten in the hopes that it will not
  9. ; only reduce the memory requirement, but also reduce the execution
  10. ; time.
  11. ;
  12. ;     This file should be assembled with Turbo Assembler.  If you need
  13. ; to use another assembler, then you should keep some things in mind.
  14. ;
  15. ; The TPASCAL memory model sets up automatically:
  16. ; * the standard Turbo Pascal entry code
  17. ;      push bp
  18. ;      mov bp,sp
  19. ; * the standard Turbo Pascal exit code
  20. ;      pop bp
  21. ;      ret n
  22. ; * the order of the arguments don't need to be reversed in the
  23. ;   assembly code.
  24. ;-----------------------------------------------------------------------
  25. ; 9001.20     Naoto Kimura
  26. ;             * Initial version created
  27. ;
  28. ; Modification history:
  29. ;
  30. ; 9005.05     Naoto Kimura
  31. ;             * Broke up assembler modules into separate files to try
  32. ;               to decrease overhead (although the overhead is pretty
  33. ;               small, it's always nice to make available whatever
  34. ;               memory the user can use).
  35. ; 9005.14     Naoto Kimura
  36. ;             * Added Strip to functions rewritten in assembler
  37. ;=======================================================================
  38. .MODEL   TPASCAL
  39. LOCALS
  40.  
  41. .CODE
  42.  
  43. ;-----------------------------------------------------------------------
  44. ;FUNCTION Copies ( Original:String; Num:Byte ) : String
  45. ;
  46. ;     This function returns as many copies of a string concatenated
  47. ; together as requested.
  48. ;
  49. ; REGISTER USAGE:  AX,BX,CX,DX,DI,SI,ES    -- Destroyed
  50. ;-----------------------------------------------------------------------
  51. Copies        PROC FAR Original:DWORD,Num:BYTE RETURNS Result:DWORD
  52.         PUBLIC    Copies
  53.         USES    ds
  54.         xor    bx,bx        ; init result string length
  55.         mov    dl,[Num]    ; find out number of times to
  56.         or    dl,dl        ; copy
  57.         jz    @@Done
  58.         cld            ; forward string op
  59.         xor    ax,ax
  60.         lds    si,[Original]    ; get addr of function parameter
  61.         lodsb            ; check length
  62.         or    ax,ax
  63.         jz    @@Done
  64.         les    di,[Result]    ; get addr of function result
  65.         inc    di        ; get past string length part
  66.         mov    cx,ax
  67.         mov    dh,cl
  68. @@Froot:    rep movsb        ; copy string
  69.         add    bl,dh        ; update length
  70.         dec    dl        ; decrement count
  71.         or    dl,dl
  72.         jz    @@Done
  73.         lds    si,[Original]
  74.         inc    si        ; skip past length byte
  75.         mov    cl,dh
  76.         mov    ax,cx
  77.         add    ax,bx
  78.         cmp    ax,00FFH
  79.         jl    @@Froot        ; copy if still less than
  80.         sub    ax,00FFH    ; find out how many not to copy
  81.         sub    cx,ax        ; adjust count
  82.         mov    al,cl
  83.         rep movsb        ; and copy
  84.         add    bl,al
  85. @@Done:        les    di,[Result]    ; set result string length
  86.         mov    [es:di],bl
  87.         ret
  88. Copies        ENDP
  89.  
  90. ;-----------------------------------------------------------------------
  91. ;FUNCTION RightJustify ( S:string; Siz:byte; Pad:char ) : string
  92. ;
  93. ;     This function returns a string that has the string "S" right
  94. ; justified in a field of length "Siz" of the character "Pad".  If the
  95. ; string is longer than the field, the string will be truncated at the
  96. ; field width.
  97. ;
  98. ; REGISTER USAGE:  AX,CX,DI,SI,ES    -- Destroyed
  99. ;-----------------------------------------------------------------------
  100. RightJustify    PROC FAR S:DWORD,Siz:BYTE,Pad:BYTE RETURNS Result:DWORD
  101.         PUBLIC    RightJustify
  102.         USES    DS
  103.         les    di,[Result]    ; get addr of function result
  104.         cld            ; forward string op
  105.         xor    ax,ax
  106.         mov    al,[Siz]    ; get field size
  107.         stosb            ; store it
  108.         mov    cx,ax
  109.         mov    al,[Pad]    ; get pad character
  110.         rep stosb        ; fill result string with pad char
  111.         les    di,[Result]    ; get addr of function result
  112.         inc    di        ; adjust to access 1st char
  113.         lds    si,[S]        ; get addr of function parameter
  114.         lodsb            ; get original string length
  115.         mov    cl,[Siz]    ; get field size
  116.         cmp    ax,cx
  117.         jge    @@Ok        ; Ok  since (length(Orig) > Siz)
  118.                     ; ax<cx  (length(orig) < Siz)
  119.         xchg    cx,ax        ; set count to be the lesser
  120.         sub    ax,cx        ; find adjustment
  121.         add    di,ax        ; and adjust destination
  122. @@Ok:        rep movsb
  123.         ret
  124. RightJustify    ENDP
  125.  
  126. ;-----------------------------------------------------------------------
  127. ;FUNCTION LeftJustify ( S:string; Siz:byte; Pad:char ) : String
  128. ;
  129. ;     This function returns a string that has the string "S"
  130. ; left justified in a field of length "Siz" of the character
  131. ; "Pad".  If the string is longer than the field, the string will
  132. ; be truncated at the field width.
  133. ;
  134. ; REGISTER USAGE:  AX,CX,DI,SI,ES    -- Destroyed
  135. ;-----------------------------------------------------------------------
  136. LeftJustify    PROC FAR S:DWORD,Siz:BYTE,Pad:BYTE RETURNS Result:DWORD
  137.         PUBLIC    LeftJustify
  138.         USES    DS
  139.         les    di,[Result]    ; get addr of function result
  140.         cld            ; forward string op
  141.         xor    ax,ax
  142.         mov    al,[Siz]    ; get field size
  143.         stosb            ; store it
  144.         mov    cx,ax
  145.         mov    al,[Pad]    ; get pad character
  146.         rep stosb        ; fill result string with pad char
  147.         lds    si,[S]        ; get addr of function parameter
  148.         lodsb            ; get original string length
  149.         les    di,[Result]    ; get addr of function result
  150.         inc    di        ; adjust to access 1st char
  151.         mov    cl,[Siz]    ; get field size
  152.         cmp    ax,cx
  153.         jge    @@Ok        ; Ok  since (length(Orig) > Siz)
  154.         ; ax<cx  (length(orig) < Siz)
  155.         xchg    cx,ax        ; set count to be the lesser
  156. @@Ok:        rep movsb
  157.         ret
  158. LeftJustify    ENDP
  159.  
  160. ;-----------------------------------------------------------------------
  161. ;FUNCTION Center ( S:string; Siz:byte; Pad:char ) : String
  162. ;
  163. ;     This function returns a string that has the string "S"
  164. ; centered in a field of length "Siz" of the character "Pad".  If
  165. ; the string is longer than the field, the string will be truncated at
  166. ; the field width.
  167. ;
  168. ; REGISTER USAGE:  AX,CX,DI,SI,ES    -- Destroyed
  169. ;-----------------------------------------------------------------------
  170. Center        PROC FAR S:DWORD,Siz:BYTE,Pad:BYTE RETURNS Result:DWORD
  171.         PUBLIC    Center
  172.         USES    DS
  173.         les    di,[Result]    ; get addr of function result
  174.         cld            ; forward string op
  175.         xor    ax,ax
  176.         mov    al,[Siz]    ; get field size
  177.         stosb            ; store it
  178.         mov    cx,ax
  179.         mov    al,[Pad]    ; get pad character
  180.         rep stosb        ; fill result with pad char
  181.         lds    si,[S]        ; get addr of function parameter
  182.         lodsb            ; get original string length
  183.         les    di,[Result]    ; get addr of function result
  184.         inc    di        ; adjust to access 1st char
  185.         mov    cl,[Siz]    ; get field size
  186.         cmp    ax,cx
  187.         jge    @@Ok        ; Ok  since (length(Orig) > Siz)
  188.                     ; ax<cx  (length(orig) < Siz)
  189.         xchg    cx,ax        ; set count to be the lesser
  190.         sub    ax,cx        ; find adjustment
  191.         shr    ax,1        ; 1/2 the adjustment
  192.         add    di,ax        ; add adjustment to destination
  193. @@Ok:        rep movsb
  194.         ret
  195. Center        ENDP
  196.  
  197. ;-----------------------------------------------------------------------
  198. ;FUNCTION Reverse ( S:String ) : String
  199. ;
  200. ;     This function returns a copy of a string that is reversed.
  201. ;
  202. ; REGISTER USAGE:  AX,CX,DI,SI,ES    -- Destroyed
  203. ;-----------------------------------------------------------------------
  204. Reverse        PROC FAR S:DWORD RETURNS Result:DWORD
  205.         PUBLIC    Reverse
  206.         USES    ds
  207.         les    di,[Result]    ; get addr of function result
  208.         lds    si,[S]        ; get addr of function parameter
  209.         xor    ax,ax
  210.         cld            ; forward string op
  211.         lodsb            ; get length
  212.         mov    [es:di],al    ; copy length
  213.         or    ax,ax        ; null string ?
  214.         jz    @@done
  215.         mov    cx,ax
  216.         add    di,cx
  217. @@copy:        lodsb            ; grab character forward
  218.         mov    [es:di],al    ; store character
  219.         dec    di        ; backward for destination
  220.         loop    @@copy
  221. @@done:        ret
  222. Reverse        ENDP
  223.  
  224. ;-----------------------------------------------------------------------
  225. ;FUNCTION Strip ( S:String;  Unwanted:String;  Location:Char ) : String
  226. ;
  227. ;     This function strips off unwanted characters from either the
  228. ; left, right or both ends of a string.
  229. ;
  230. ; REGISTER USAGE:  AX,CX,DX,DI,SI,ES    -- Destroyed
  231. ;-----------------------------------------------------------------------
  232. Strip        PROC FAR S:DWORD,U:DWORD,L:BYTE RETURNS Result:DWORD
  233.         PUBLIC    Strip
  234.         USES    ds
  235.         LOCAL    Skip:BYTE,EndSkip:BYTE
  236.         mov    [Skip],0
  237.         mov    [EndSkip],0
  238.         mov    al,[L]
  239.         cmp    al,'r'
  240.         je    @@IsArr
  241.         cmp    al,'R'
  242.         je    @@IsArr
  243.         cmp    al,'b'
  244.         je    @@IsArr
  245.         cmp    al,'B'
  246.         jne    @@TestEll
  247. @@IsArr:    sub    ax,ax
  248.         mov    cx,ax
  249.         lds    si,[S]
  250.         mov    al,[si]        ; get length of original
  251.         mov    bx,ax
  252.         add    si,bx        ; move to end of original string
  253.         les    di,[U]
  254.         mov    cl,[es:di]    ; get length of unwanted
  255.         or    ax,cx
  256.         jz    @@DoCopy    ; if either are zero, then done
  257.         mov    dx,di
  258. @@RightStrip:    inc    di        ; LOOP ; move past length byte
  259.         std            ;   backward string op
  260.         lodsb            ;   check char
  261.         cld            ;   forward string op
  262.         repne scasb        ;   scan
  263.         jne    @@TestEll    ; EXIT IF NOT Found
  264.         inc    [EndSkip]    ;   increment skip count
  265.         dec    bx        ;   decrease copy length
  266.         or    bx,bx        ;
  267.         jz    @@DoCopy    ; EXIT IF NullString
  268.         mov    di,dx        ;   reset search string
  269.         mov    cl,[es:di]    ;   get length again
  270.         jmp    @@RightStrip    ; ENDLOOP
  271. @@TestEll:    mov    al,[L]
  272.         cmp    al,'l'
  273.         je    @@IsEll
  274.         cmp    al,'L'
  275.         je    @@IsEll
  276.         cmp    al,'b'
  277.         je    @@IsEll
  278.         cmp    al,'B'
  279.         jne    @@DoCopy
  280. @@IsEll:    sub    cx,cx
  281.         mov    ax,cx
  282.         les    di,[U]
  283.         mov    dx,di
  284.         mov    cl,[es:di]    ; get length of unwanted string
  285.         lds    si,[S]
  286.         lodsb            ; get length of original string
  287.         mov    bx,ax
  288.         sub    bl,[Skip]    ; adjust for skipped chars
  289.         or    ax,cx
  290.         jz    @@DoCopy    ; if either is zero, then done
  291.         cld            ; forward string op
  292. @@LeftStrip:    inc    di        ; LOOP ;move past length byte
  293.         lodsb            ;   get char to check
  294.         repne scasb        ;   scan
  295.         jne    @@DoCopy    ; EXIT IF NOT Found
  296.         inc    [Skip]        ;   increment skip count
  297.         dec    bx        ;   decrease copy length
  298.         or    bx,bx        ;
  299.         jz    @@DoCopy    ; EXIT IF NullString
  300.         mov    di,dx        ;   reset search string
  301.         mov    cl,[es:di]    ;   get length again
  302.         jmp    @@LeftStrip    ; ENDLOOP
  303. @@DoCopy:    lds    si,[S]
  304.         les    di,[Result]
  305.         sub    ax,ax
  306.         sub    bx,bx
  307.         lodsb
  308.         mov    bl,[Skip]
  309.         add    si,bx
  310.         sub    al,bl
  311.         sub    al,[EndSkip]
  312.         stosb
  313.         mov    cx,ax
  314.         jcxz    @@Done
  315.         cld            ; forward string op
  316.         rep movsb
  317. @@Done:        ret
  318. Strip        ENDP
  319.  
  320. END
  321.